import 'package:flutter/material.dart';
import 'package:interview/components/app/app_animated_overlay.dart';
import 'package:interview/components/app/app_ripple_button.dart';
import 'package:interview/providers/theme_providers.dart';
import 'package:provider/provider.dart';

class SettingThemeOverlay {
  static void show(BuildContext context) {
    final provider = context.read<ThemeProvider>();
    OverlayState overlayState = Overlay.of(context);
    OverlayEntry? overlayEntry;

    overlayEntry = OverlayEntry(
      builder: (context) {
        return AppAnimatedOverlay(
          onClose: () {
            overlayEntry!.remove();
          },
          content: _buildThemeContent(context, provider), // 动态内容
        );
      },
    );

    overlayState.insert(overlayEntry);
  }

  static void hide(){
    AppAnimatedOverlay.close();
  }

  static Widget _buildThemeContent(BuildContext context, ThemeProvider provider) {
    return Container(
      width: MediaQuery.of(context).size.width * 0.88,
      decoration: BoxDecoration(
        color: Theme.of(context).cardColor,
        borderRadius: BorderRadius.circular(12),
      ),
      padding: const EdgeInsets.symmetric(vertical: 8, horizontal: 16),
      child: Column(
        mainAxisSize: MainAxisSize.min,
        children: [
          _buildSwitchTile(context, provider),
          AppRippleButton(
            onTap: () {
              provider.switchTheme("light");
            },
            padding: const EdgeInsets.only(right: 4, top: 12, bottom: 12),
            child: _buildOptionTile(
              title: '浅色模式',
              isSystem: provider.themeMode == ThemeMode.system,
              isSelect: provider.themeMode == ThemeMode.light,
            ),
          ),
          AppRippleButton(
            onTap: () {
              provider.switchTheme("dark");
            },
            padding: const EdgeInsets.only(right: 4, top: 12, bottom: 12),
            child: _buildOptionTile(
              title: '深色模式',
              isSystem: provider.themeMode == ThemeMode.system,
              isSelect: provider.themeMode == ThemeMode.dark,
            ),
          ),
        ],
      ),
    );
  }

  static Widget _buildSwitchTile(BuildContext context, ThemeProvider provider) {
    return Row(
      mainAxisAlignment: MainAxisAlignment.spaceBetween,
      children: [
        Text(
          "跟随系统",
          style: TextStyle(fontSize: 13, fontWeight: FontWeight.w500),
        ),
        Transform.scale(
          scale: 0.5,
          child: Switch(
            value: provider.themeMode == ThemeMode.system,
            onChanged: (value) {
              if (value) {
                provider.switchTheme("system");
              } else {
                provider.switchTheme("light");
              }
            },
            activeColor: Colors.orange,
          ),
        ),
      ],
    );
  }

  static Widget _buildOptionTile({
    required String title,
    required bool isSelect,
    required bool isSystem,
  }) {
    return Row(
      mainAxisAlignment: MainAxisAlignment.spaceBetween,
      children: [
        Text(
          title,
          style: TextStyle(fontSize: 13, fontWeight: FontWeight.w500),
        ),
        if (isSystem) Icon(Icons.block, size: 14),
        if (isSelect) Icon(Icons.check, size: 14),
      ],
    );
  }
}

